import { fileURLToPath, URL } from 'node:url';
import { transformAssetUrls } from '@quasar/vite-plugin';
import Vue from '@vitejs/plugin-vue';
import { QuasarResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    Vue({
      template: { transformAssetUrls },
    }),
    Components({
      resolvers: [QuasarResolver()],
    }),
    dts({
      insertTypesEntry: true,
      outDir: './dist/types',
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('src', import.meta.url)),
    },
    extensions: ['.js', '.json', '.jsx', '.mjs', '.ts', '.tsx', '.vue'],
  },
  build: {
    lib: {
      entry: fileURLToPath(new URL('./src/index.ts', import.meta.url)),
      name: '@herodotus-cloud/bpmn-designer',
      fileName: (format) => (format === 'es' ? `index.${format}.mjs` : `index.${format}.js`),
    },
    minify: 'terser',
    terserOptions: {
      // 生产环境下移除console
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
      keep_classnames: true,
    },
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      external: [
        '@bpmn-io/element-template-chooser',
        '@herodotus-cloud/bpmn-apis',
        '@herodotus-cloud/components',
        '@herodotus-cloud/core',
        '@highlightjs/vue-plugin',
        'bpmnlint',
        'bpmnlint/rules/conditional-flows',
        'bpmnlint/rules/end-event-required',
        'bpmnlint/rules/event-sub-process-typed-start-event',
        'bpmnlint/rules/fake-join',
        'bpmnlint/rules/label-required',
        'bpmnlint/rules/no-bpmndi',
        'bpmnlint/rules/no-complex-gateway',
        'bpmnlint/rules/no-disconnected',
        'bpmnlint/rules/no-duplicate-sequence-flows',
        'bpmnlint/rules/no-gateway-join-fork',
        'bpmnlint/rules/no-implicit-split',
        'bpmnlint/rules/no-inclusive-gateway',
        'bpmnlint/rules/single-blank-start-event',
        'bpmnlint/rules/single-event-definition',
        'bpmnlint/rules/start-event-required',
        'bpmnlint/rules/sub-process-blank-start-event',
        'bpmnlint/rules/superfluous-gateway',
        'bpmn-js',
        'bpmn-js/lib/Modeler',
        'bpmn-js/lib/Viewer',
        'bpmn-js/lib/util/DiUtil',
        'bpmn-js/lib/util/DrilldownUtil',
        'bpmn-js/lib/util/ModelUtil',
        'bpmn-js-bpmnlint',
        'bpmn-js-connectors-extension',
        'bpmn-js-properties-panel',
        'bpmn-js-token-simulation',
        'bpmn-moddle',
        'camunda-bpmn-moddle',
        'camunda-bpmn-moddle/resources/camunda',
        'diagram-js',
        'diagram-js/lib/util/Collections',
        'diagram-js-grid',
        'diagram-js-minimap',
        'highlight.js',
        'highlight.js/lib/core',
        'highlight.js/lib/languages/xml',
        'highlight.js/lib/languages/json',
        'pinia',
        'quasar',
        'vue',
      ],
      output: {
        exports: 'named',
        assetFileNames: `assets/[ext]/[name][extname]`,
        // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
        globals: {
          '@bpmn-io/element-template-chooser': 'BpmnIoElementTemplateChooser',
          '@herodotus-cloud/bpmn-apis': 'HerodotusBpmnApis',
          '@herodotus-cloud/components': 'HerodotusComponents',
          '@herodotus-cloud/core': 'HerodotusCore',
          '@highlightjs/vue-plugin': 'HighlightjsVuePlugin',
          bpmnlint: 'Bpmnlint',
          'bpmnlint/rules/conditional-flows': 'BpmnlintRulesConditionalFlows',
          'bpmnlint/rules/end-event-required': 'BpmnlintRulesEndEventRequired',
          'bpmnlint/rules/event-sub-process-typed-start-event':
            'BpmnlintRulesEventSubProcessTypedStartEvent',
          'bpmnlint/rules/fake-join': 'BpmnlintRulesFakeJoin',
          'bpmnlint/rules/label-required': 'BpmnlintRulesLabelRequired',
          'bpmnlint/rules/no-bpmndi': 'BpmnlintRulesNoBpmndi',
          'bpmnlint/rules/no-complex-gateway': 'BpmnlintRulesNoComplexGateway',
          'bpmnlint/rules/no-disconnected': 'BpmnlintRulesNoDisconnected',
          'bpmnlint/rules/no-duplicate-sequence-flows': 'BpmnlintRulesNoDuplicateSequenceFlows',
          'bpmnlint/rules/no-gateway-join-fork': 'BpmnlintRulesNoGatewayJoinFork',
          'bpmnlint/rules/no-implicit-split': 'BpmnlintRulesNoImplicitSplit',
          'bpmnlint/rules/no-inclusive-gateway': 'BpmnlintRulesNoInclusiveGateway',
          'bpmnlint/rules/single-blank-start-event': 'BpmnlintRulesSingleBlankStartEvent',
          'bpmnlint/rules/single-event-definition': 'BpmnlintRulesSingleEventDefinition',
          'bpmnlint/rules/start-event-required': 'BpmnlintRulesStartEventRequired',
          'bpmnlint/rules/sub-process-blank-start-event': 'BpmnlintRulesSubProcessBlankStartEvent',
          'bpmnlint/rules/superfluous-gateway': 'BpmnlintRulesSuperfluousGateway',
          'bpmn-js': 'BpmnJs',
          'bpmn-js/lib/Modeler': 'BpmnJsLibModeler',
          'bpmn-js/lib/Viewer': 'BpmnJsLibViewer',
          'bpmn-js/lib/util/DiUtil': 'BpmnJsLibUtilDiUtil',
          'bpmn-js/lib/util/DrilldownUtil': 'BpmnJsLibUtilDrilldownUtil',
          'bpmn-js/lib/util/ModelUtil': 'BpmnJsLibUtilModelUtil',
          'bpmn-js-bpmnlint': 'BpmnJsBpmnlint',
          'bpmn-js-connectors-extension': 'BpmnJsConnectorsExtension',
          'bpmn-js-properties-panel': 'BpmnJsPropertiesPanel',
          'bpmn-js-token-simulation': 'BpmnJsTokenSimulation',
          'bpmn-moddle': 'BpmnModdle',
          'camunda-bpmn-moddle': 'CamundaBpmnModdle',
          'camunda-bpmn-moddle/resources/camunda': 'CamundaBpmnModdleResourcesCamunda',
          'diagram-js': 'DiagramJs',
          'diagram-js/lib/util/Collections': 'DiagramJsLibUtilCollections',
          'diagram-js-grid': 'DiagramJsGrid',
          'diagram-js-minimap': 'DiagramJsMinimap',
          'highlight.js': 'HighlightJs',
          'highlight.js/lib/core': 'HighlightJsLibCore',
          'highlight.js/lib/languages/xml': 'HighlightJslibLanguagesXml',
          'highlight.js/lib/languages/json': 'HighlightJsLibLanguagesJson',
          pinia: 'pinia',
          quasar: 'quasar',
          vue: 'vue',
        },
      },
    },
  },
});
